/*
 * Description: 
 *     History: yang@haipo.me, 2017/03/29, create
 */

# include "me_config.h"
# include "me_trade.h"

static dict_plus<const char*, market_t*> *dict_market;
static dict_plus<const char*, asset_type*> *dict_asset;

int init_trade(void)
{
    dict_market = dict_plus<const char*, market_t*>::create(64);
    if (dict_market == NULL)
        return -__LINE__;

    for (size_t i = 0; i < settings.market_num; ++i) {
        market_t *market = market_t::create(&settings.markets[i]);
        if (market == NULL) {
            return -__LINE__;
        }
        dict_market->add(settings.markets[i].name, market);
    }

    dict_asset = dict_plus<const char *, asset_type*>::create(64);
    if (dict_asset == NULL)
        return -__LINE__;

    for (size_t i = 0; i < settings.asset_num; ++i) {
        asset_type type;
        type.prec_save = settings.assets[i].prec_save;
        type.prec_show = settings.assets[i].prec_show;

        if (dict_asset->add( settings.assets[i].name, &type) == NULL)
            return -__LINE__;
    }
    return 0;
}

market_t *get_market(const char *name)
{
    auto entry = dict_market->find(name);
    if (entry)
        return entry->get_value();
    return NULL;
}

asset_type *get_asset_type(const char *asset)
{
    auto entry = dict_asset->find(asset);
    if (entry == NULL)
        return NULL;

    return entry->get_value();
}

bool asset_exist(const char *asset)
{
    asset_type *at = get_asset_type(asset);
    return at ? true : false;
}

int asset_prec(const char *asset)
{
    asset_type *at = get_asset_type(asset);
    return at ? at->prec_save : -1;
}

int asset_prec_show(const char *asset)
{
    asset_type *at = get_asset_type(asset);
    return at ? at->prec_show: -1;
}
